package cmd

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"

	"github.com/olekukonko/tablewriter"
	"github.com/sirupsen/logrus"

	"gitee.com/xuanmingyi/tinystack/model"

	"strconv"
)

func base_url() string {
	return "http://localhost:8000"
}

func get(s string) []byte {
	resp, err := http.Get(fmt.Sprintf("%s%s", base_url(), s))
	if err != nil {
		logrus.Fatal(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	return body
}

func list_images() {
	body := get("/images/")
	var images []model.Image

	err := json.Unmarshal(body, &images)
	if err != nil {
		logrus.Fatal(err)
	}
	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"ID", "Name", "Uuid", "Checksum", "Status"})
	for _, image := range images {
		table.Append([]string{strconv.Itoa(image.Id), image.Name, image.Uuid,
			image.Checksum, image.Status})
	}
	table.Render()
}

func add_images() {
}
